package cn.byapps.master.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cn.byapps.master.entity.SysUser;
import cn.byapps.master.service.UserService;
import cn.byapps.master.vo.ListPageVO;
import cn.byapps.master.vo.UserVO;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class SysUserController {

	@Autowired
	private UserService userService;

	@GetMapping("get/user/info")
	public Map<String,Object> info(String token) {
		Map<String,Object> map = new HashMap<>();
		SysUser sysUser = userService.getByUser("admin");
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(sysUser, userVO);
		userVO.setRoles(Arrays.asList(sysUser.getRoles().split(",")));
		map.put("success", "1");
		map.put("message", "mess");
		map.put("data", userVO);
		return map;
	}

	@PostMapping("post/getUsersFromPage")
	public Map<String, Object> listUser(Integer page, Boolean skip) {
		Integer rows = 10;
		ListPageVO vo = userService.getPageVO(page, rows);
		Map<String, Object> map = new HashMap<>();
		map.put("success", "1");
		map.put("message", "12345678");
		map.put("allDataLength", vo.getCount());
		map.put("users", vo.getUsers());
		return map;
	}

	@PostMapping("post/adminRegister")
	public Map<String, Object> addUser(@RequestBody UserVO vo) {
		Map<String, Object> map = new HashMap<>();
		userService.save(vo);
		map.put("success", "1");
		map.put("message", "12345678");
		System.out.println(vo);
		return map;
	}

	@PostMapping("post/updatesomerole")
	public Map<String, Object> updateUser(@RequestBody UserVO vo) {
		Map<String, Object> map = new HashMap<>();
		userService.update(vo);
		map.put("success", "1");
		map.put("message", "12345678");
		System.out.println(vo);
		return map;
	}
	
	
	@PostMapping("post/deleteuser")
	public Map<String, Object> deleteUser(@RequestBody UserVO vo) {
		Map<String, Object> map = new HashMap<>();
		userService.delete(vo);
		map.put("success", "1");
		map.put("message", "12345678");
		System.out.println(vo);
		return map;
	}
}
